<?php

namespace App\Http\Controllers\admin;

use App\model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\admin\Controller;
use App\model\Login_ope;


class LoginController extends Controller
{
    public function login(Request $request){
        if($request->all()){
            $arr=$request->all();
            $model=new User();
            $user=$model->login($arr);
            if(!empty($user)&&isset($user)){
                $user=$user->toArray();
                $id=$user['use_id'];
                $username=$user['use_name'];
                $url=$model->onePower($id);
                session(['rbac' =>$url]);
                session(['use_id' =>$id]);
				session(['use_name' =>$user['use_name']]);
                $ip=$request->getClientIp();
                $login=new Login_ope();
				$login->loginHistory($ip,$user['use_name']);
                return view('index',compact('username'));
            }else{
                $error='账号或密码错误';
                return view('login',compact('error'));
            }
        }else{
            $error=false;
            return view('login',compact('error'));
        }
    }
    public function logout(Request $request){
        $login=new Login_ope();
		$use_name=$request->session()->get('use_name');
        $log_time=$request->session()->get('log_time');
		$login->quitHistory($use_name,$log_time);
        $request->session()->flush();
        return redirect('/');
    }
}
